//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    typeTabs: [
      { name: "猪年拜年卡", isActive: true },
      { name: "元宵祝福卡", isActive: false }
    ],
    cardId: '',
    tapArea: '',
    tabIndex: 0,
    chooseTabIndex: 0,
    modelLists1: [],
    modelLists2: []
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  tabActive: function (event) {
    console.log(event);
    this.setData({
      tabIndex: event.currentTarget.dataset.index
    });
    if (this.data.hasUserInfo) {
      this.setData({
        typeTabs: [{ name: "猪年拜年卡", isActive: false }, { name: "元宵祝福卡", isActive: false }]
      });
      if (this.data.tabIndex === 0) {
        this.setData({
          'typeTabs[0].isActive': true,
          chooseTabIndex: 0
        });
        if (this.data.modelLists1.length == '0') {
          this.getModelLists(this.data.tabIndex + 1);
        }
      } else {
        this.setData({
          'typeTabs[1].isActive': true,
          chooseTabIndex: 1
        });
        if (this.data.modelLists2.length == '0') {
          this.getModelLists(this.data.tabIndex + 1);
        }
      }
    } else {
      // 等待获取授权信息
    }
  },
  onLoad: function () {
    var _self = this;
    if (app.globalData.userInfo) {
      console.log(this.data.userInfo);
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      });
      //_self.getModelLists(_self.data.chooseTabIndex + 1);
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
        // _self.getModelLists(_self.data.chooseTabIndex + 1);
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          });
          //_self.getModelLists(_self.data.chooseTabIndex + 1);
        }
      })
    }
    _self.getModelLists(_self.data.chooseTabIndex + 1);
  },
  getUserInfo: function (e) {
    this.tabArea = e.currentTarget.dataset.area;
    if (e.detail.userInfo) { // 同意授权
      app.globalData.userInfo = e.detail.userInfo;
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      });
      console.log(this.data.userInfo);
      if (this.tabArea === "tab") {
        // this.getModelLists(this.data.chooseTabIndex + 1);
        this.setData({
          typeTabs: [{ name: "猪年拜年卡", isActive: false }, { name: "元宵祝福卡", isActive: false }]
        });
        if (this.data.tabIndex === 0) {
          this.setData({
            'typeTabs[0].isActive': true,
            chooseTabIndex: 0
          });
          if (this.data.modelLists1.length == '0') {
            this.getModelLists(this.data.chooseTabIndex + 1);
          }
        } else if (this.data.tabIndex === 1) {
          this.setData({
            'typeTabs[1].isActive': true,
            chooseTabIndex: 1
          });
          if (this.data.modelLists2.length == '0') {
            this.getModelLists(this.data.chooseTabIndex + 1);
          }
        }
      } else if (this.tabArea === "card") {
        wx.navigateTo({
          url: '../fgrh/fgrh'
        });
      }
    } else {// 拒绝
      console.log(e);
    }

  },
  // 获取模板列表
  getModelLists: function (type) {
    var _self = this;
    wx.request({
      url: 'https://mini.xiutub.com/home/api/alist?type=' + type,
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log(res.data)
        if (type === 1) {
          _self.setData({ // 新年
            modelLists1: res.data.res
          });
          //wx.setStorageSync('modelLists1', res.data.res);
        } else {
          _self.setData({ // 元宵
            modelLists2: res.data.res
          });
          //wx.setStorageSync('modelLists2', res.data.res);
        }
      }
    })
  },
  
  // 选取模板
  makeCard: function (event) { console.log(event);
    //console.log(event.currentTarget.dataset);
    this.setData({
      cardId: event.currentTarget.dataset.id
    })
    var which = event.currentTarget.dataset.which.substr(-8).substring(0 , 4);
    console.log(which);
    if (this.data.hasUserInfo) {
      wx.setStorageSync("cardId", this.data.cardId);
      wx.navigateTo({
        url: '../' + which + '/' + which + '?cardId=' + this.data.cardId,
      });
    } else {
      
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '个性拜年就用拜年卡',
      path: '/pages/index/index'
    }

  },

})
